package com.lcb.storage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcb.common.utils.BaseApiService;
import com.lcb.common.utils.BaseResponse;
import com.lcb.storage.entity.TStorage;
import com.lcb.storage.mapper.TStorageMapper;
import com.lcb.storage.service.TStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/****
 * @Author:lcb
 * @Description:TStorage业务层接口实现类
 * @Date: 2021-01-28 11:29:40
 *****/

@RestController
//@RequestMapping("/storage")
//@CrossOrigin
public class TStorageServiceImpl extends BaseApiService implements TStorageService {

    @Autowired
    private TStorageMapper tStorageMapper;

    @Override
    public BaseResponse decrease(@RequestBody TStorage storage) {

        storage.setId(1l);
        storage.setTotal(50);

        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        tStorageMapper.updateByPrimaryKeySelective(storage);
        return setResultSuccess("扣减成功");
    }

    /**
     * TStorage条件+分页查询
     *
     * @param tStorage 查询条件
     * @param page     页码
     * @param size     页大小
     * @return 分页结果
     */
    @Override
    public BaseResponse findTStorageList(@RequestBody TStorage tStorage, @PathVariable(value = "page") int page, @PathVariable(value = "size") int size) {
        //分页
        PageHelper.startPage(page, size);

        PageInfo<TStorage> pagelist = new PageInfo<TStorage>(tStorageMapper.select(tStorage));

        return setResultSuccess(pagelist);
    }

    /**
     * 删除
     *
     * @param ids
     */
    @Override
    public BaseResponse deleteTStorageById(@PathVariable(value = "ids") String ids) {

        String[] split = ids.split(",");
        Example example = new Example(TStorage.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", Arrays.asList(split));
        tStorageMapper.deleteByExample(example);
        return setResultSuccess("删除成功");
    }

    /**
     * 修改TStorage
     *
     * @param tStorage
     */
    @Override
    public BaseResponse updateTStorage(@RequestBody TStorage tStorage) {
        tStorageMapper.updateByPrimaryKeySelective(tStorage);
        return setResultSuccess("修改成功");
    }

    /**
     * 增加TStorage
     *
     * @param tStorage
     */
    @Override
    public BaseResponse addTStorage(@RequestBody TStorage tStorage) {
        tStorageMapper.insertSelective(tStorage);
        return setResultSuccess("添加成功");
    }

    /**
     * 根据ID查询TStorage
     *
     * @param id
     * @return
     */
    @Override
    public BaseResponse findTStorageById(@PathVariable(value = "id") Long id) {
        return setResultSuccess(tStorageMapper.selectByPrimaryKey(id));
    }

    /**
     * 查询TStorage全部数据
     *
     * @return
     */
    @Override
    public BaseResponse findTStorageAll() {
        return setResultSuccess(tStorageMapper.selectAll());
    }
}
